package com.example.luckin.slice.fraction;

import com.example.luckin.ResourceTable;
import com.example.luckin.slice.entity.MainItem;
import com.example.luckin.slice.provider.MainItemProvider;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;
import java.util.List;


public class MainFraction extends Fraction {

    private Component component;
    private Button btnScan;
    private PageSlider pageSlider;
    private PageSliderIndicator pageSliderIndicator;

    private Button btnTakeOut;
    private Button btnPickUp;
    // 定义线程
    private EventHandler mHandler = new EventHandler(EventRunner.create());
    private int mDelayedTime = 3000;// Banner 切换时间间隔

    private ListContainer listContainer;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        component = scatter.parse(ResourceTable.Layout_fraction_main, container, false);
        return component;
    }

    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();
        mHandler.removeTask(runnable);
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        initComponent();
        initCarousel();
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            int mCurrentItem = pageSlider.getCurrentPage();
            mCurrentItem++;
            if (mCurrentItem >= 3) {
                mCurrentItem = 0;
            }
            int finalMCurrentItem = mCurrentItem;
            getFractionAbility().getContext().getUITaskDispatcher().asyncDispatch(() -> {
                pageSlider.setCurrentPage(finalMCurrentItem);
                mHandler.postTask(runnable, mDelayedTime);
            });
        }
    };

    private void initCarousel() {
        btnScan = (Button) component.findComponentById(ResourceTable.Id_btn_scan);

        pageSlider = (PageSlider) component.findComponentById(ResourceTable.Id_ps_adv);

        List<Component> list = new ArrayList<>(0);

        Image image = new Image(getFractionAbility().getContext());
        image.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);
        image.setHeight(DependentLayout.LayoutConfig.MATCH_PARENT);
        image.setPixelMap(ResourceTable.Media_adv01);
        image.setScaleMode(Image.ScaleMode.STRETCH);
        list.add(image);

        Image image2 = new Image(getFractionAbility().getContext());
        image2.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);
        image2.setHeight(DependentLayout.LayoutConfig.MATCH_PARENT);
        image2.setPixelMap(ResourceTable.Media_adv02);
        image2.setScaleMode(Image.ScaleMode.STRETCH);
        list.add(image2);

        Image image3 = new Image(getFractionAbility().getContext());
        image3.setWidth(DependentLayout.LayoutConfig.MATCH_PARENT);
        image3.setHeight(DependentLayout.LayoutConfig.MATCH_PARENT);
        image3.setPixelMap(ResourceTable.Media_adv03);
        image3.setScaleMode(Image.ScaleMode.STRETCH);
        list.add(image3);

        pageSlider.setProvider(new PageSliderProvider() {
            @Override
            public int getCount() {
                return list.size();
            }

            @Override
            public Object createPageInContainer(ComponentContainer componentContainer, int i) {
                Component component = list.get(i);
                componentContainer.addComponent(component);
                return component;
            }

            @Override
            public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
                componentContainer.removeComponent(list.get(i));
            }

            @Override
            public boolean isPageMatchToObject(Component component, Object o) {
                return component == o;
            }
        });

        pageSliderIndicator = (PageSliderIndicator) component.findComponentById(ResourceTable.Id_psi_adv);
        pageSliderIndicator.setViewPager(pageSlider);

        mHandler.postTask(runnable, mDelayedTime);
    }

    // 初始化组件
    private List<MainItem> items = new ArrayList<>(0);

    private void initComponent() {

        // 选择按钮，由两个按钮组成
        btnTakeOut = (Button) component.findComponentById(ResourceTable.Id_btn_takeout);
        btnPickUp = (Button) component.findComponentById(ResourceTable.Id_btn_pickup);
        btnTakeOut.setClickedListener(e -> {
            btnTakeOut.setBackground(null);
            btnTakeOut.setTextColor(Color.BLACK);
            Element shapeElement = new ShapeElement(getFractionAbility().getContext(), ResourceTable.Graphic_switch_select);
            btnPickUp.setBackground(shapeElement);
            btnPickUp.setTextColor(Color.WHITE);
        });
        btnPickUp.setClickedListener(e -> {
            btnPickUp.setBackground(null);
            btnPickUp.setTextColor(Color.BLACK);
            Element shapeElement = new ShapeElement(getFractionAbility().getContext(), ResourceTable.Graphic_switch_select);
            btnTakeOut.setBackground(shapeElement);
            btnTakeOut.setTextColor(Color.WHITE);
        });

        items.add(new MainItem(ResourceTable.Media_coffee, "现在下单", "ORDER NOW"));
        items.add(new MainItem(ResourceTable.Media_wallet, "咖啡钱包", "COFFEE WALLET"));
        items.add(new MainItem(ResourceTable.Media_gift, "送Ta咖啡", "SEND COFFEE"));
        items.add(new MainItem(ResourceTable.Media_enterprise, "企业账号", "ENTERPRISE ACCOUNT"));

        listContainer = (ListContainer) component.findComponentById(ResourceTable.Id_list_main);
        // 设置数据源
        listContainer.setItemProvider(new MainItemProvider(this.getFractionAbility().getContext(), items));
    }

}
